//
//  MineHeaderView.m
//  Btpin
//
//  Created by Vic on 2019/3/17.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "MineHeaderView.h"
#import "MineHeaderItemView.h"

@interface MineHeaderView ()
{
    UILabel *nameLbl;
    UIImageView *headerImgView;
    
    MineHeaderItemView *resumeView;
    MineHeaderItemView *deliverView;
    MineHeaderItemView *invateView;
    MineHeaderItemView *attentionView;
}
@end

@implementation MineHeaderView

- (instancetype)initWithTarget:(id)target roleType:(IDType)type {
    self = [super init];
    if (self) {
        [self initUIRoleType:type];
        self.delegate = target;
    }
    return self;
}
- (void)initUIRoleType:(IDType)type {
    UIView *switchView = [UIToolsControl initViewWithFrame:CGRectMake(0, 35*Scale+((isiPhoneX||isiPhoneXr||isiPhoneXsMax)?ScalePS(20):0), 80*Scale, 22*Scale) backgroundColor:THEMECOLOR_OF_ORANGE];
    [self addSubview:switchView];
    [switchView corners:(UIRectCornerTopRight | UIRectCornerBottomRight) radii:CGSizeMake(11*Scale, 11*Scale)];
    UIImageView *switchImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(5*Scale, 5*Scale, 12*Scale, 12*Scale) imageName:@"role_switch"];
    [switchView addSubview:switchImgView];
    UILabel *switchLbl = [UIToolsControl initLabelWithFrame:CGRectMake(EndX(switchImgView)+5*Scale, 0, 55*Scale, ViewHeight(switchView)) text:((type == IDTypeOfCompany)?@"我要求职":@"我要招人") textColor:COLOR_OF_WHITE font:Font_12];
    [switchView addSubview:switchLbl];
    
    UIButton *switchBtn = [UIToolsControl initButtonWithFrame:CGRectMake(0, ((isiPhoneX||isiPhoneXr||isiPhoneXsMax)?ScalePS(20):0), 100*Scale, 55*Scale) imageName:nil target:self action:@selector(switchBtnTap) title:nil titleColor:nil];
    [self addSubview:switchBtn];
    
//    NSString *nameStr = ((type==IDTypeOfWorker)?(UD_GetOForK(isUserName)?UD_GetOForK(isUserName):@"比特聘友"):(([DataToolsControl sharedManager].companyBaseInfoModel && [DataToolsControl sharedManager].companyBaseInfoModel.userinfo.name) ?[DataToolsControl sharedManager].companyBaseInfoModel.userinfo.name:@"比特聘友"));
    NSString *nameStr = ((type==IDTypeOfWorker)?(UD_GetOForK(isNickname)?UD_GetOForK(isNickname):@"比特聘友"):(UD_GetOForK(isCompanyUserInfoName)?UD_GetOForK(isCompanyUserInfoName):@"比特聘友"));
    if (nameStr && nameStr.length) {
        
    } else {
        nameStr = @"比特聘友";
    }
    nameLbl = [UIToolsControl initLabelWithFrame:CGRectMake(30*Scale, EndY(switchView)+15*Scale, 200, 37*Scale) text:nameStr textColor:THEMECOLOR_OF_TEXT_333333 font:Font_27];
    [self addSubview:nameLbl];
    UIImageView *editImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(StartX(nameLbl), EndY(nameLbl)+7*Scale, 16*Scale, 15*Scale) imageName:@"mine_edit"];
    [self addSubview:editImgView];
    UILabel *editLbl = [UIToolsControl initLabelWithFrame:CGRectMake(EndX(editImgView)+8*Scale, StartY(editImgView), 200, ViewHeight(editImgView)) text:((type == IDTypeOfCompany)?@"编辑招聘人身份":@"编辑基本资料") textColor:THEMECOLOR_OF_ORANGE font:Font_13];
    [self addSubview:editLbl];
    
    headerImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(WIDTH-108*Scale, 60*Scale, 78*Scale, 78*Scale) imageName:@"header_bg"];
    headerImgView.contentMode = UIViewContentModeScaleAspectFill;
    headerImgView.layer.cornerRadius = 39*Scale;
    headerImgView.layer.masksToBounds = YES;
    [self addSubview:headerImgView];
    if (type == IDTypeOfWorker) {
        if (UD_GetOForK(isAvatar)) {
            [headerImgView sd_setImageWithURL:[NSURL URLWithString:UD_GetOForK(isAvatar)] placeholderImage:[UIImage imageNamed:@"header_bg"]];
        }
    } else {
//        if ([DataToolsControl sharedManager].companyBaseInfoModel.userinfo.avatar) {
//            [headerImgView sd_setImageWithURL:[NSURL URLWithString:(([DataToolsControl sharedManager].companyBaseInfoModel.userinfo.avatar&&[DataToolsControl sharedManager].companyBaseInfoModel.userinfo.avatar.length)?[DataToolsControl sharedManager].companyBaseInfoModel.userinfo.avatar:UD_GetOForK(isAvatar))] placeholderImage:[UIImage imageNamed:@"header_bg"]];
//        }
        if (UD_GetOForK(isCompanyUserInfoAvatar)) {
            [headerImgView sd_setImageWithURL:[NSURL URLWithString:UD_GetOForK(isCompanyUserInfoAvatar)] placeholderImage:[UIImage imageNamed:@"header_bg"]];
        }
    }
    
    UIButton *baseInfoBtn = [UIToolsControl initButtonWithFrame:CGRectMake(0, EndY(switchBtn), WIDTH, 85*Scale) imageName:nil target:self action:@selector(baseInfoBtnTap) title:nil titleColor:nil];
    [self addSubview:baseInfoBtn];
    
    CGFloat btnWidth = (WIDTH-10*Scale)*0.25;
    resumeView = [[MineHeaderItemView alloc] initWithFrame:CGRectMake(5*Scale, EndY(baseInfoBtn)+15*Scale, btnWidth, PSCellHeight) text:((type == IDTypeOfCompany)?@"初试沟通":@"我的简历")];
    [resumeView addTarget:self action:@selector(itemTap:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:resumeView];
    resumeView.count = @"0";
    
    deliverView = [[MineHeaderItemView alloc] initWithFrame:CGRectMake(5*Scale+btnWidth, StartY(resumeView), btnWidth, ViewHeight(resumeView)) text:((type == IDTypeOfCompany)?@"已发邀请":@"我的投递")];
    [deliverView addTarget:self action:@selector(itemTap:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:deliverView];
    
    invateView = [[MineHeaderItemView alloc] initWithFrame:CGRectMake(5*Scale+btnWidth*2, StartY(resumeView), btnWidth, ViewHeight(resumeView)) text:((type == IDTypeOfCompany)?@"简历管理":@"复试邀请")];
    [invateView addTarget:self action:@selector(itemTap:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:invateView];
    
    attentionView = [[MineHeaderItemView alloc] initWithFrame:CGRectMake(5*Scale+btnWidth*3, StartY(resumeView), btnWidth, ViewHeight(resumeView)) text:((type == IDTypeOfCompany)?@"人才收藏":@"收藏关注")];
    [attentionView addTarget:self action:@selector(itemTap:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:attentionView];
    
    UIView *lineView = [UIToolsControl initViewWithFrame:CGRectMake(0, EndY(resumeView), WIDTH, 10*Scale) backgroundColor:THEMECOLOR_OF_GRAY];
    [self addSubview:lineView];
    
    if (type == IDTypeOfWorker) {
        [self setWorkerPartBaseInfo];
    } else {
        [self setCompanyPartBaseInfo];
    }
}
- (void)setWorkerPartBaseInfo {
    resumeView.count = (UD_GetOForK(isResumePercent)?[NSString stringWithFormat:@"%@%@", UD_GetOForK(isResumePercent), @"%"]:@"10%");
    deliverView.count = (UD_GetOForK(isApplyNum)?UD_GetOForK(isApplyNum):@"0");
    invateView.count = (UD_GetOForK(isInterviewNum)?UD_GetOForK(isInterviewNum):@"0");
    if (UD_GetOForK(isNewInterview)) {
        invateView.hiddenDoc = ([UD_GetOForK(isNewInterview) integerValue])?NO:YES;
    }
    attentionView.count = (UD_GetOForK(isFocusNum)?UD_GetOForK(isFocusNum):@"0");
    resumeView.hiddenDoc = YES;
}
- (void)setCompanyPartBaseInfo {
//    resumeView.count = ([DataToolsControl sharedManager].companyBaseInfoModel.count_info.apply_num)?([DataToolsControl sharedManager].companyBaseInfoModel.count_info.apply_num):@"0";
    deliverView.count = ([DataToolsControl sharedManager].companyBaseInfoModel.count_info.interview_num)?([DataToolsControl sharedManager].companyBaseInfoModel.count_info.interview_num):@"0";
    invateView.count = ([DataToolsControl sharedManager].companyBaseInfoModel.count_info.apply_num)?([DataToolsControl sharedManager].companyBaseInfoModel.count_info.apply_num):@"0";
    attentionView.count = ([DataToolsControl sharedManager].companyBaseInfoModel.count_info.favorite_num)?([DataToolsControl sharedManager].companyBaseInfoModel.count_info.favorite_num):@"0";
    resumeView.hiddenDoc = YES;
}
- (void)switchBtnTap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(mineHeaderViewTap:)]) {
        [self.delegate mineHeaderViewTap:MineHeaderViewTapTypeOfRoleSwitch];
    }
}
- (void)setHeaderImgUrl:(NSString *)headerImgUrl {
    if (headerImgUrl && headerImgUrl.length) {
        [headerImgView sd_setImageWithURL:[NSURL URLWithString:headerImgUrl] placeholderImage:[UIImage imageNamed:@"header_bg"]];
    }
}
- (void)baseInfoBtnTap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(mineHeaderViewTap:)]) {
        [self.delegate mineHeaderViewTap:MineHeaderViewTapTypeOfEditInfo];
    }
}
- (void)itemTap:(MineHeaderItemView *)item {
    if (self.delegate && [self.delegate respondsToSelector:@selector(mineHeaderViewTap:)]) {
        if (item == resumeView) {
            [self.delegate mineHeaderViewTap:MineHeaderViewTapTypeOfResume];
            resumeView.hiddenDoc = YES;
        } else if (item == deliverView) {
            [self.delegate mineHeaderViewTap:MineHeaderViewTapTypeOfDeliver];
            deliverView.hiddenDoc = YES;
        } else if (item == invateView) {
            [self.delegate mineHeaderViewTap:MineHeaderViewTapTypeOfInvate];
            invateView.hiddenDoc = YES;
        } else {
            [self.delegate mineHeaderViewTap:MineHeaderViewTapTypeOfAttention];
            attentionView.hiddenDoc = YES;
        }
    }
}
- (void)setResumeCount:(NSString *)resumeCount {
    resumeView.count = resumeCount;
}
- (void)setDeliverCount:(NSString *)deliverCount {
    deliverView.count = deliverCount;
}
- (void)setInvateCount:(NSString *)invateCount {
    invateView.count = invateCount;
}
- (void)setAttentionCount:(NSString *)attentionCount {
    attentionView.count = attentionCount;
}
- (void)setHiddenResumeDoc:(BOOL)hiddenResumeDoc {
    resumeView.hiddenDoc = hiddenResumeDoc;
}
- (void)setHiddenDeliverDoc:(BOOL)hiddenDeliverDoc {
    deliverView.hiddenDoc = hiddenDeliverDoc;
}
- (void)setHiddenInvateDoc:(BOOL)hiddenInvateDoc {
    invateView.hiddenDoc = hiddenInvateDoc;
}
- (void)setHiddenAattentionDoc:(BOOL)hiddenAattentionDoc {
    attentionView.hiddenDoc = hiddenAattentionDoc;
}

@end
